package com.yixin.consumer;

import org.aspectj.weaver.ast.Var;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

@RestController
public class UserHelloController {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    @Qualifier("restTemplateOne")
    RestTemplate restTemplateOne;
    @Autowired
    @Qualifier("restTemplate")
    RestTemplate restTemplate;

    int count = 0;

    @GetMapping("/hello5")
    public String hello5() {
        String zhuzhu = restTemplate.getForObject("http://provider/hello2?name={1}", String.class, "zhuzhu");
        ResponseEntity<String> zhuzhu1 = restTemplate.getForEntity("http://provider/hello2?name={1}", String.class, "zhuzhu");
        zhuzhu1.getBody();
        zhuzhu1.getStatusCode();
        zhuzhu1.getStatusCodeValue();
        zhuzhu1.getHeaders();

        return zhuzhu;
    }

    @GetMapping("/hello4")
    public String hello4() {
        return restTemplate.getForObject("http://provider/hello", String.class);
    }

    @GetMapping("/hello3")
    public String hello3() {
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance instance = list.get(count++ % list.size());
        String host = instance.getHost();
        int port = instance.getPort();
        StringBuffer sb = new StringBuffer();
        sb.append("http://")
                .append(host).append(":")
                .append(port).append("/hello");
        String s = restTemplateOne.getForObject(sb.toString(), String.class);
        return s;
    }


    @GetMapping("/hello2")
    public String hello2() {
        List<ServiceInstance> list = discoveryClient.getInstances("provider");
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        HttpURLConnection con = null;

        try {
            StringBuffer sb = new StringBuffer();
            sb.append("http://")
                    .append(host).append(":")
                    .append(port).append("/hello");
            URL url = new URL(sb.toString());
            con = (HttpURLConnection) url.openConnection();
            if (con.getResponseCode() == 200) {
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = br.readLine();
                br.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "cuole";
    }

    //写死
    @GetMapping("/hello")
    public String hello1() {
        HttpURLConnection con = null;
        try {
            URL url = new URL("http://localhost:1113/hello");
            con = (HttpURLConnection) url.openConnection();
            if (con.getResponseCode() == 200) {
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String s = br.readLine();
                br.close();
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "cuole";
    }
}
